﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using System.Data;
using TimKen.DataAccess;
using System.Data.SqlClient;

namespace TimKen.Web.Admin.Help
{
    public partial class HelpClass_Manage : AdminPage 
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
                Bind();
            }

        }



        protected void btSave_Click(object sender, EventArgs e)
        {
          
            string classNameCn = this.txtClassNameCn.Text;
            string classNameEn = this.txtClassNameEn.Text;
            


         

            string classId = this.lablClassID.Text;

            if (string.IsNullOrEmpty(classId))
            {
                  string insertsql = "INSERT INTO HelpClass (ClassNameCn,ClassNameEn) VALUES('{0}','{1}')";
                  insertsql = string.Format(insertsql,classNameCn,classNameEn);
                  DbHelperSQL.ExecuteSql(insertsql);
            }
            else
            {
               string updatesql = "UPDATE HelpClass SET  ClassNameCN='{0}',ClassNameEn='{1}' WHERE ClassID={2}";
               updatesql = string.Format(updatesql,classNameCn,classNameEn,classId);
               DbHelperSQL.ExecuteSql(updatesql);

            }

            Bind();
        }

        protected void Bind()
        {

            string  sql = "select  * from HelpClass";
            DataSet ds = DbHelperSQL.Query(sql);

            this.gvHelpClassList.DataSource = ds.Tables[0].DefaultView;
            this.gvHelpClassList.DataBind();
             
        }

      

        protected void gvHelpClassList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {

                string sql = "delete from HelpClass WHERE ClassID={0}";
                sql = string.Format(sql, int.Parse(e.CommandArgument.ToString()));

                DataSet ds = DbHelperSQL.Query(sql);

                Bind();
            }
            else if (e.CommandName == "mod")
            {
                string sql = "select *  FROM   HelpClass WHERE ClassID={0}";
                sql = string.Format(sql, int.Parse(e.CommandArgument.ToString()));

                SqlDataReader  reader = DbHelperSQL.ExecuteReader(sql);
                   
               if(reader.Read()){
                   this.lablClassID.Text = reader["ClassID"].ToString();
                   this.txtClassNameCn.Text = reader["ClassNameCn"].ToString();
                   this.txtClassNameEn.Text = reader["ClassNameEn"].ToString();
               }
               reader.Close();
               
            }
        }

        protected void BindResources()
        {
            btnSave.Text = GetLangString("Save");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.lablClassID.Text = "";
            this.txtClassNameCn.Text = "";
            this.txtClassNameEn.Text = "";
        }
    }
}
